package com.esd.csair;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;

import org.apache.log4j.Logger;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.util.FalsifyingWebConnection;

public class SimpleConectionListener extends FalsifyingWebConnection implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8168493020295282906L;



	



	private static Logger log = Logger.getLogger(SimpleConectionListener.class);

	public String URLloginFunction = null;
	public String URLPassengerInformation = null;
	public String URLorderQuery = null;
	public String URLcsairlogin = null;
	// public String URLCsairbaseInfo = null;
	public String URLpay = null;
	public String URLpayComplete = null;
	public String URLpayErro = null;
	public String URLcsairConnectInfoLoad = null;
	public String URLcsairConnectInfojs = null;
	public String URLaddressAction = null;
	public String URLdropdownAction = null;
	public String URLsaveskypearlAction = null;
	public String URLModifyPasswordcheck = null;
	public String URLorderDetail = null;
	public String URLOrderMXError = null;
	public String URLAssigneeCtrl = null;
	public String URLAssigneeCtrljs = null;
	public String URLCheckYzm = null;
	public String URLorderCN = null;
	public String URLorderCN2 = null;
	public String URLflight = null;
	public String URLflightWF = null;
	public String URLyecode = null;
	public String URLyeregister = null;
	public String URLyexianshi = null;
	public String URLchinapayNext = null;
	public String URLrechargeableNext = null;
	public String URLYHQsy = null;
	public String URLMileagetotalPage = null;
	public String URLChecklogin = null;
	public String URLCityLCLjMX = null;
	public String URLLCExchangeMX = null;
	public String URLflightDynamicAo = null;
	public String URLsaveCommContactor = null;
	public String EditCommContactorPage = null;
	public String EditCommContactor = null;
	public String viewCommPassengerPage = null;
	public String URLsaveCommPassenger = null;
	public String editCommPassengerPage = null;
	public String URLlcExchangeList = null;
	public String searchAgent = null;
	public String tableJson = null;
	public String CsairSearchPage = null;
	public String URLfindFlight = null;
	public String onlineIdentityAuthentication = null;
	public String addAssignee = null;
	public String change2 = null;
	public String change3 = null;
	public String getChangeNo = null;
	public String getSvcEPassenger = null;
	public String lcljcxymjz = null;
	public String couponRemarkLoad = null;
	public String loadAlienee = null;
	public String refundSuceess = null;
//	public String  findAlienee = null;
	public String aaaa = null;
	public String payYzm = null;
	public Object bbbb = null;
	
	public Object cccc = null;







	







	






	
	
	public static void main(String[] args) {
		String path = System.getProperty("user.dir");
		System.out.println(path);
	}
	
	
	public SimpleConectionListener(WebClient webClient) {
		super(webClient);
	}

	@Override
	public WebResponse getResponse(WebRequest request) throws IOException {
		WebResponse response = super.getResponse(request);
		String url = response.getWebRequest().getUrl().toString();
		long time = response.getLoadTime();
		String type = response.getContentType();
		String content = response.getContentAsString();
		int statusCode = response.getStatusCode();
		String msg = response.getStatusMessage();
		response.getWebRequest().getEncodingType();
		if(url.startsWith(Csair.cccc)) {
			log.info("url====>"+url);
			log.info("Content====>"+content);
		}
		log.info("[url:" + url + "][time:" + time + "][type:" + type + "][statusCode:" + statusCode + "][msg:" + msg
				+ "]");

		if (url.endsWith("topMenu.js")) {
			return createWebResponse(response.getWebRequest(), "", "application/javascript", 200, "Ok");
		}
		if (url.endsWith("DD_belatedPNG_0.0.7a-min.js")) {
			return createWebResponse(response.getWebRequest(), "", "application/javascript", 200, "Ok");
		}
		if (url.startsWith("https://static.gridsumdissector.com")) {
			return createWebResponse(response.getWebRequest(), "", "application/javascript", 200, "Ok");
		}
		if (url.startsWith("http://b2c.csair.com/B2C40/payBill/WalletPay/jaxb/getAccInfo.ao")) {
			return createWebResponse(response.getWebRequest(), "", "text/html", 200, "Ok");
		}
		if (url.startsWith("http://v2.jiathis.com/code/jia.js")) {
			return createWebResponse(response.getWebRequest(), "", "application/xml", 200, "Ok");
		}
//		if (url.endsWith("analysis.min.js")) {
//			return createWebResponse(response.getWebRequest(), "", "application/javascript", 200, "Ok");
//		}
		
//		if (url.endsWith("wtid.js")) {
//			log.info("-------wtid.js-------");
//			return createWebResponse(response.getWebRequest(), "", "application/x-javascript", 200, "Ok");
//		}
//		if (url.endsWith("sdc_gd.js")) {
//			log.info("-------sdc_gd.js-------");
//			return createWebResponse(response.getWebRequest(), "", "application/x-javascript", 200, "Ok");
//		}
//		if (url.endsWith("discountInfo")) {
//			log.info("-------discountInfo-------");
//			return createWebResponse(response.getWebRequest(), "", "application/json", 200, "Ok");
//		}
		
		if (type.equals("text/css")) {
			
			//return createWebResponse(response.getWebRequest(), "fewfwefe", "text/css", 200, "Ok");
			//return replaceContent(response.getWebRequest(),"alert('fjeowjfoejwofjoiwejof')");
			
			
//			String s = response.getContentAsString();
//			int beginIndex = url.lastIndexOf("/");
//			int endIndex = url.lastIndexOf("?");
//			String cssName = url.substring(beginIndex+1, endIndex);
//			File f = new File("D:/cache/css/"+cssName);
//			if(f.isFile()) {
//				System.out.println("找到缓存文件");
//	            StringBuffer sb= new StringBuffer();
//	            FileReader reader = new FileReader(f);
//	            BufferedReader br = new BufferedReader(reader);
//	            String str = null;
//	            while((str = br.readLine()) != null) {
//	                  sb.append(str+"/n");
//	            } 
//	            br.close();
//	            reader.close();
//				return createWebResponse(response.getWebRequest(), sb.toString(), "text/css", 200, "Ok");
//			}
//			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
//			bos.write(s.getBytes());
//			bos.flush();
//			bos.close();
//			System.out.println(s);
			
			
		}

		handle(url);
		return response;
	}

	public void handle(String url) {
		if (url.startsWith(Csair.URLloginFunction)) {
			URLloginFunction = url;
		}
		if (url.startsWith(Csair.URLpassengerInformation)) {
			URLPassengerInformation = url;
		}
		if (url.startsWith(Csair.URLPassengerInformationLC)) {
			URLPassengerInformation = url;
		}
		if (url.startsWith(Csair.URLaddressAction)) {
			URLaddressAction = url;
		}

		if (url.startsWith(Csair.URLorderQuery)) {
			URLorderQuery = url;
		}

		if (url.startsWith(Csair.URLcsairlogin)) {
			URLcsairlogin = url;
		}
		// if (url.startsWith(Csair.URLCsairbaseInfo)) {
		// URLCsairbaseInfo = url;
		// }

		if (url.equals(Csair.URLpay)) {
			
			URLpay = url;
		}
//		if (url.startsWith(Csair.URLpay2)) {
//			URLpay = url;
//		}

		if (url.startsWith(Csair.URLpayComplete)) {
			URLpayComplete = url;
		}
		if (url.startsWith(Csair.URLpayComplete1)) {
			URLpayComplete = url;
		}

		if (url.endsWith(Csair.URLpayErro)) {
			URLpayErro = url;
		}
		if (url.startsWith(Csair.URLCsairConnectInfoLoad)) {
			URLcsairConnectInfoLoad = url;
		}
		if (url.startsWith(Csair.URLdropdownAction)) {
			URLdropdownAction = url;
		}
		if (url.startsWith(Csair.URLsaveskypearlAction)) {
			URLsaveskypearlAction = url;
		}
		if (url.startsWith(Csair.URLModifyPasswordcheck)) {
			URLModifyPasswordcheck = url;
		}
		if (url.startsWith(Csair.URLorderDetail)) {
			URLorderDetail = url;
		}
		if (url.startsWith(Csair.URLOrderMXError)) {
			URLOrderMXError = url;
		}
		if (url.startsWith(Csair.URLAssigneeCtrl)) {
			URLAssigneeCtrl = url;
		}
		if (url.startsWith(Csair.URLAssigneeCtrljs)) {
			URLAssigneeCtrljs = url;
		}
		if (url.startsWith(Csair.URLCheckYzm)) {
			URLCheckYzm = url;
		}
		if (url.startsWith(Csair.URLCheckYzm1)) {
			URLCheckYzm = url;
		}
		if (url.startsWith(Csair.URLorderCN)) {
			URLorderCN = url;
		}
		if (url.startsWith(Csair.URLorderCN2)) {
			URLorderCN2 = url;
		}
		if (url.startsWith(Csair.URLflight)) {
			URLflight = url;
		}
		if (url.startsWith(Csair.URLflight2)) {
			URLflight = url;
		}
		
		if (url.startsWith(Csair.URLflightWF)) {
			URLflightWF = url;
		}
		if (url.startsWith(Csair.URLyecode)) {
			URLyecode = url;
		}
		if (url.startsWith(Csair.URLyeregister)) {
			URLyeregister = url;
		}
		if (url.startsWith(Csair.URLyexianshi)) {
			URLyexianshi = url;
		}
		if (url.startsWith(Csair.URLchinapayNext)) {
			URLchinapayNext = url;
		}
		if (url.startsWith(Csair.URLrechargeableNext)) {
			URLrechargeableNext = url;
		}
		if (url.startsWith(Csair.URLYHQsy)) {
			URLYHQsy = url;
		}
		if (url.startsWith(Csair.URLYHQsy2)) {
			URLYHQsy = url;
		}
		if (url.startsWith(Csair.URLMileagetotalPage)) {
			URLMileagetotalPage = url;
		}
		if (url.startsWith(Csair.URLCityLCLjMX)) {
			URLCityLCLjMX = url;
		}
		if (url.startsWith(Csair.URLLCExchangeMX)) {
			URLLCExchangeMX = url;
		}
		if (url.startsWith(Csair.URLflightDynamicAo)) {
			URLflightDynamicAo = url;
		}
		if (url.startsWith(Csair.URLsaveCommContactor)) {
			URLsaveCommContactor = url;
		}
		if (url.startsWith(Csair.EditCommContactorPage)) {
			EditCommContactorPage = url;
		}
		if (url.startsWith(Csair.EditCommContactor)) {
			EditCommContactor = url;
		}
		if (url.startsWith(Csair.viewCommPassengerPage)) {
			viewCommPassengerPage = url;
		}
		if (url.startsWith(Csair.URLsaveCommPassenger)) {
			URLsaveCommPassenger = url;
		}
		if (url.startsWith(Csair.editCommPassengerPage)) {
			editCommPassengerPage = url;
		}
		if (url.startsWith(Csair.URLlcExchangeList)) {
			URLlcExchangeList = url;
		}
		if (url.startsWith(Csair.searchAgent)) {
			searchAgent = url;
		}
		if (url.startsWith(Csair.tableJson)) {
			tableJson = url;
		}
		if (url.startsWith(Csair.CsairSearchPage)) {
			CsairSearchPage = url;
		}
		if (url.startsWith(Csair.URLfindFlight)) {
			URLfindFlight = url;
		}
		if (url.startsWith(Csair.onlineIdentityAuthentication)) {
			onlineIdentityAuthentication = url;
		}
		if (url.startsWith(Csair.addAssignee)) {
			addAssignee = url;
		}
		if (url.startsWith(Csair.change2)) {
			change2 = url;
		}
		if (url.startsWith(Csair.change3)) {
			change3 = url;
		}
		if (url.startsWith(Csair.getChangeNo)) {
			getChangeNo = url;
		}
		if (url.startsWith(Csair.getChangeNo1)) {
			getChangeNo = url;
		}
		if (url.startsWith(Csair.getSvcEPassenger)) {
			getSvcEPassenger = url;
		}
		if (url.startsWith(Csair.lcljcxymjz)) {
			lcljcxymjz = url;
		}
		if (url.startsWith(Csair.lcljcxymjz1)) {
			lcljcxymjz = url;
		}
		if (url.startsWith(Csair.lcljcxymjz2)) {
			lcljcxymjz = url;
		}
		if (url.startsWith(Csair.lcljcxymjz3)) {
			lcljcxymjz = url;
		}
		if (url.startsWith(Csair.couponRemarkLoad)) {
			couponRemarkLoad = url;
		}
		if (url.startsWith(Csair.payYzm)) {
			payYzm = url;
		}
		if (url.startsWith(Csair.loadAlienee)) {
			loadAlienee = url;
		}
		if (url.startsWith(Csair.refundSuceess)) {
			refundSuceess = url;
		}
//		if (url.startsWith(Csair.findAlienee)) {
//			findAlienee = url;
//		}
		if (url.startsWith(Csair.aaaa)) {
			aaaa = url;
		}
		if (url.startsWith(Csair.bbbb)) {
			bbbb = url;
		}
		if (url.startsWith(Csair.cccc)) {
			cccc = url;
		}
		
	}

	@Override
	protected WebResponse createWebResponse(WebRequest wr, String content, String contentType) throws IOException {

		return super.createWebResponse(wr, content, contentType);
	}

	@Override
	protected WebResponse createWebResponse(WebRequest wr, String content, String contentType, int responseCode,
			String responseMessage) throws IOException {
		log.info("=======================createWebResponse=================================================");
		log.info("[content:" + content + "][contentType:" + contentType + "]" + "[responseCode:" + responseCode + "]"
				+ "[responseMessage:" + responseMessage + "]");
		return super.createWebResponse(wr, content, contentType, responseCode, responseMessage);
	}

	@Override
	protected WebResponse replaceContent(WebResponse wr, String newContent) throws IOException {
		log.info("=======================replaceContent=================================================");
		log.info("[wr:" + wr + "][newContent" + newContent + "]");
		return super.replaceContent(wr, newContent);
	}

	@Override
	protected WebResponse deliverFromAlternateUrl(WebRequest webRequest, URL url) throws IOException {
		log.info("=======================deliverFromAlternateUrl=================================================");
		log.info("[webRequest:" + webRequest + "][url" + url + "]");
		return super.deliverFromAlternateUrl(webRequest, url);
	}

}
